package com.example.administrator.listview_test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ArrayList<Photo> photoList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for( int i = 0 ; i < 5 ; i++ ) {
            Photo photo = new Photo(R.drawable.image_test, "图：" +i );
            photoList.add(photo);
        }
        PhotoAdapter photoAdapter = new PhotoAdapter(MainActivity.this, R.layout.photo_item, photoList);
        ListView listView = (ListView) findViewById(R.id.lv_test);
        listView.setAdapter(photoAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Photo photo = photoList.get(position);
                Toast.makeText(MainActivity.this, photo.getName(), Toast.LENGTH_SHORT).show();
            }
        });

    }
}
